﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLNSTL
{
    public partial class frmMain : Form
    {
        Form1 DSNV = null;
        frmDSKhenThuong DSKT = null;
        frmDSNghiPhep DSNP = null;
        frmDSKyLuat DSKL = null;
        frmPhongBan frmPB = null;

        //User
        User user = null;
        private User USER
        {
            set
            {
                if (value != null)
                {
                    if (value.Ma_Loai_User == 2)
                    {
                        //Enable menus
                        menuStrip1.Items["nhânSựToolStripMenuItem"].Visible = true;
                        menuStrip1.Items["tiềnLươngToolStripMenuItem"].Visible = true;
                        menuStrip1.Items["thôngSốToolStripMenuItem"].Visible = true;
                        this.đăngXuấtToolStripMenuItem.Visible = true;
                        this.đổiPasswordToolStripMenuItem.Visible = true;
                        this.đăngNhậpToolStripMenuItem.Visible = false;
                        
                        //Load status bar
                    }
                }
                else
                {
                    //Tắt form
                    TatForm(DSNV);
                    TatForm(DSNP);
                    TatForm(DSKT);
                    TatForm(DSKL);
                    TatForm(frmPB);
                    TatForm(frmCV);
                    DSNV = null;
                    DSNP = null;
                    DSKL = null;
                    DSKT = null;
                    frmPB = null;
                    frmCV = null;

                    //
                    //Enable menus
                    menuStrip1.Items["nhânSựToolStripMenuItem"].Visible = false;
                    menuStrip1.Items["tiềnLươngToolStripMenuItem"].Visible = false;
                    menuStrip1.Items["thôngSốToolStripMenuItem"].Visible = false;
                    this.đăngXuấtToolStripMenuItem.Visible = false;
                    this.đổiPasswordToolStripMenuItem.Visible = false;
                    this.đăngNhậpToolStripMenuItem.Visible = true;
                }
                user = value;
            }
        }

        private void TatForm(Form f)
        {
            if (f!=null)
            {
                try
                {
                    f.Close();
                    f.Dispose();
                }
                catch (Exception)
                {

                    
                }
            }
        }

        public frmMain()
        {
            InitializeComponent();
        }

        #region Menu Nhân sự
        private void nhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DSNV == null)
            {
                DSNV = new Form1();
                DSNV.MdiParent = this;
                DSNV.FormClosed += new FormClosedEventHandler(DSNV_FormClosed);
                DSNV.Show(); 
            }
        }

        private void DestroyForm(Form f)
        {
            if (f != null)
            {
                f.Dispose();
            }
        }

        void DSNV_FormClosed(object sender, FormClosedEventArgs e)
        {
            DestroyForm(DSNV);
            DSNV = null;
        }

        private void nghỉPhépToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DSNP == null)
            {
                DSNP = new frmDSNghiPhep();
                DSNP.MdiParent = this;
                DSNP.FormClosed += new FormClosedEventHandler(DSNP_FormClosed);
                DSNP.Show(); 
            }
        }

        void DSNP_FormClosed(object sender, FormClosedEventArgs e)
        {
            DestroyForm(DSNP);
            DSNP = null;
        }

        private void khenThưởngVàKỷLuậtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DSKT == null)
            {
                DSKT = new frmDSKhenThuong();
                DSKT.MdiParent = this;
                DSKT.FormClosed += new FormClosedEventHandler(DSKT_FormClosed);
                DSKT.Show(); 
            }
        }

        void DSKT_FormClosed(object sender, FormClosedEventArgs e)
        {
            DestroyForm(DSKT);
            DSKT = null;
        }

        private void kỷLuậtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DSKL == null)
            {
                DSKL = new frmDSKyLuat();
                DSKL.MdiParent = this;
                DSKL.FormClosed += new FormClosedEventHandler(DSKL_FormClosed);
                DSKL.Show(); 
            }
        }

        void DSKL_FormClosed(object sender, FormClosedEventArgs e)
        {
            DestroyForm(DSKL);
            DSKL = null;
        }
        #endregion

        private void DangNhap()
        {
            Login l = new Login();
            if (l.ShowDialog() == DialogResult.OK)
            {
                this.USER = l.USER;
            }
            else
            {
                this.USER = null;
            }
        }

        private void đăngNhậpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DangNhap();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            DangNhap();
        }

        private void đăngXuấtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            USER = null;
        }

        private void đổiPasswordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (user != null)
            {
                frmDoiPass dp = new frmDoiPass(user.Username);
                dp.ShowDialog();
            }
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void phòngBanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmPB == null)
            {
                frmPB = new frmPhongBan();
                frmPB.MdiParent = this;
                frmPB.FormClosed += new FormClosedEventHandler(frmPB_FormClosed);
                frmPB.Show(); 
            }
        }

        void frmPB_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmPB.Dispose();
            frmPB = null;
        }

        frmChucVu frmCV = null;
        private void chứcVụToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmCV == null)
            {
                frmCV = new frmChucVu();
                frmCV.MdiParent = this;
                frmCV.FormClosed += new FormClosedEventHandler(frmCV_FormClosed);
                frmCV.Show();
            }
        }
        void frmCV_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmCV.Dispose();
            frmCV = null;
        }

        private void quốcTịchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var frmtsQT = new frmTSQuocTich();
            frmtsQT.ShowDialog();
        }

        private void dânTộcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var frmtsQT = new frmTSDanToc();
            frmtsQT.ShowDialog();
        }

        private void tônGiáoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var frmtsQT = new frmTSTonGiao();
            frmtsQT.ShowDialog();
        }

        private void tìnhTrạngHônNhânToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var frmtsQT = new frmTSTTHonNhan();
            frmtsQT.ShowDialog();
        }

        private void trìnhĐộVănHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var frmtsQT = new frmTSTrinhDoHocVan();
            frmtsQT.ShowDialog();
        }
    }
}
